﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using JULONG.WEB.Library;
using YHWE.Web.Models;

namespace YHWE.Web.Areas.manage.Controllers
{
    using Models;

        public class userTypeController : Controller
        {
            DataSession<entityDB2Container, userType> db = new DataSession<entityDB2Container, userType>();


            public userTypeController()
            {
            }

            public ActionResult Index()
            {
                ViewData.Model = db.query();
                return View();
            }

            //
            // GET: /manage/user/Create
            public ActionResult Create()
            {
                ViewData.Model = new manageUser();
                return View();
            }

            //
            // POST: /manage/user/Create

            [HttpPost]
            public ActionResult Create(userType entity)
            {

                try
                {
                    // TODO: Add insert logic here
                    db.insert(entity);
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(entity);
                }
            }

            //
            // GET: /manage/user/Edit/5

            public ActionResult Edit(int id)
            {
                ViewData.Model = db.get(d => d.ID == id);
                return View();
            }

            //
            // POST: /manage/user/Edit/5

            [HttpPost]
            public ActionResult Edit(int id, FormCollection collection)
            {
                var entity = db.get(d => d.ID == id);
                try
                {
                    UpdateModel(entity);
                    db.save();
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(entity);
                }
            }

            //
            // GET: /manage/user/Delete/5

            public ActionResult Delete(int id)
            {

                var entity = db.get(d => d.ID == id);



                if (entity.requirement)
                {
                    ModelState.AddModelError("info", "该记录系统标记为不可删除");
                    return View(entity);
                }

                try
                {
                    db.delete(entity);
                    return RedirectToAction("Index");
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("info", e.Message);
                    return View(entity);
                }
            }

        }




}
